package cn.itcast.contorller.role;

import cn.itcast.base.BaseContorller;
import cn.itcast.domain.Module;
import cn.itcast.domain.Role;
import cn.itcast.service.module.ModuleService;
import cn.itcast.service.role.RoleService;
import cn.itcast.utils.UtilFuns;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@RequestMapping(value = "/system/role")
public class RoleController extends BaseContorller {
    @Autowired
    private RoleService roleService;
    @Autowired
    private ModuleService moduleService;
    @RequestMapping(value = "/list.do",name = "查询所有的角色")
    public String findAllRole(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "5") int pageSize){
        PageInfo allRole = roleService.findAllRole(pageNum, pageSize, company_id);
        request.setAttribute("pb",allRole);
        System.out.println(allRole);
        return "/system/role/role-list";
    }
    @RequestMapping(value = "/toAdd",name = "到新增页面")
    public  String toAdd(){
    return "/system/role/role-add";
    }
    @RequestMapping(value = "/edit.do",name = "新增角色")
    public String edit(Role role){
        role.setCompanyId(company_id);
        role.setCompanyName(company_name);
        if (UtilFuns.isEmpty(role.getId())){
            role.setId(UUID.randomUUID().toString());
             roleService.save(role);
        }else {
            roleService.update(role);
        }
        return "redirect:/system/role/list.do";
    }
    @RequestMapping(value = "/toUpdate",name = "修改的回显")
    public String toUpdate(String id){
        Role role = roleService.findById(id);
        request.setAttribute("role",role);
        return "/system/role/role-update";
    }
    @RequestMapping(value = "/delete",name = "删除角色")
    public String delete(String id){
        roleService.delete(id);
        return "redirect:/system/role/list.do";
    }
    @RequestMapping(value = "/roleModule",name = "到角色分配权限的页面")
    public String roleModule(String roleid){
        Role role = roleService.findById(roleid);
        request.setAttribute("role",role);
        System.out.println(role);
        return "system/role/role-module";
    }
    @RequestMapping(value = "/initZtree",name = "ztree数据整理展示")
    public @ResponseBody List<Map>   initZtree(String rid){
        /*查询所有的权限*/
        List<Module> allModule = moduleService.findAll();
        //查询当前点击角色所具备的所有权限 rid (角色的所有权限)
       List<Module> roleModule= moduleService.findRoleModule(rid);
        // 自定义给ajax的list<Map> 用于封装ztree树的数据
       List<Map> list=new ArrayList<>();
        for (Module module : allModule) {
            Map map=new HashMap();
            map.put("id",module.getId());
            map.put("pId",module.getParentId());
            map.put("name",module.getName());
            if (roleModule.contains(module)){
                map.put("checked",true);//让ztree的复选框选中
            }
            list.add(map);
        }
        // 将自定义的List<Map>转成json格式：[{id:111, pId:11, name:"叶子节点111"},{ id:111, pId:11, name:"叶子节点111"}]
        return list;
    }
    @RequestMapping(value = "/updateRoleModule",name = "保存角色的权限")
    public String  updateRoleModule(String roleid,String moduleIds){
        moduleService.updateRoleModule(roleid,moduleIds);
        return "redirect:/system/role/list.do";
    }

}
